//
//  RecHeaderView.swift
//

import FSPagerView

class RecHeaderView: UIView {

    var banners: [RecChildModel] = [] {
        didSet {
            pagerView.reloadData()
            pageControl.numberOfPages = banners.count
        }
    }
    
    var items: [RecChildModel] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
    
    private lazy var pagerView: FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "FSPagerViewCell")
        pagerView.itemSize = pagerView.frame.size
        pagerView.isInfinite = true
        pagerView.scrollDirection = .horizontal
        pagerView.removesInfiniteLoopForSingleItem = true
        pagerView.automaticSlidingInterval = 5
        return pagerView
    }()

    private lazy var pageControl: FSPageControl = {
        let pageControl = FSPageControl()
        pageControl.hidesForSinglePage = true
        pageControl.contentHorizontalAlignment = .center
        pageControl.setFillColor(UIColor.theme, for: .selected)
        pageControl.setFillColor(UIColor.c999999, for: .normal)
        pageControl.currentPage = 0
        return pageControl
    }()
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let itemWidth = (UIScreen.width - space * 10) / 5
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth + 45.fit)
        layout.minimumLineSpacing = space * 2
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: space, bottom: 0, right: space)
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.sysTableView
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RecHeaderItemCell.self)
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(pagerView)
        addSubview(pageControl)
        addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(pagerView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        pagerView.frame = CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.width * 9 / 16)
        pageControl.frame = CGRect(x: 0, y: pagerView.frame.height - 20, width: pagerView.frame.width, height: 20)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension RecHeaderView: FSPagerViewDataSource {
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return banners.count
    }

    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "FSPagerViewCell", at: index)
        cell.imageView?.kf_set(banners[index].viewP.focusUrl)
        return cell
    }
}

extension RecHeaderView: FSPagerViewDelegate {
    public func pagerViewDidScroll(_ pagerView: FSPagerView) {
        pageControl.currentPage = pagerView.currentIndex
    }

    public func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: false)
        
    }
}

extension RecHeaderView: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: RecHeaderItemCell.self, for: indexPath)
//        cell.iconView.kf_set(items[indexPath.item].viewP.iconUrl)
//        cell.titleLabel.text = items[indexPath.item].title
        return cell
    }
}
